﻿using System;
using System.ComponentModel.DataAnnotations;

namespace OurWedding1.Models
{
    public partial class ConfirmationKey
    {
        public ConfirmationKey()
        {
        }

        public ConfirmationKey(GuestGroup group)
        {
            GuestGroupId = group.GuestGroupId;
            Key = GeneratePassword(10, 0);
            isReleased = false;
            isConfirmed = false;
        }
              

        public static string GeneratePassword(int lenght, int nonAlphaNumericChars)
        {
            const string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            const string allowedNonAlphaNum = "!@#$%^&*()_-+=[{]};:<>|./?";
            var rd = new Random();

            if (nonAlphaNumericChars > lenght || lenght <= 0 || nonAlphaNumericChars < 0)
                throw new ArgumentOutOfRangeException();

            var pass = new char[lenght];
            var pos = new int[lenght];
// ReSharper disable TooWideLocalVariableScope
// ReSharper disable RedundantAssignment
            int i = 0, j = 0;
// ReSharper restore RedundantAssignment
// ReSharper restore TooWideLocalVariableScope
// ReSharper disable TooWideLocalVariableScope
// ReSharper disable RedundantAssignment
            bool flag = false;
// ReSharper restore RedundantAssignment
// ReSharper restore TooWideLocalVariableScope

            //Random the position values of the pos array for the string Pass
            while (i < lenght - 1)
            {
// ReSharper disable RedundantAssignment
                j = 0;
// ReSharper restore RedundantAssignment
                flag = false;
                var temp = rd.Next(0, lenght);
                for (j = 0; j < lenght; j++)
                    if (temp == pos[j])
                    {
                        flag = true;
                        j = lenght;
                    }

                if (!flag)
                {
                    pos[i] = temp;
                    i++;
                }
            }

            //Random the AlphaNumericChars
            for (i = 0; i < lenght - nonAlphaNumericChars; i++)
                pass[i] = allowedChars[rd.Next(0, allowedChars.Length)];

            //Random the NonAlphaNumericChars
            for (i = lenght - nonAlphaNumericChars; i < lenght; i++)
                pass[i] = allowedNonAlphaNum[rd.Next(0, allowedNonAlphaNum.Length)];

            //Set the sorted array values by the pos array for the rigth posistion
            var sorted = new char[lenght];
            for (i = 0; i < lenght; i++)
                sorted[i] = pass[pos[i]];

            return new String(sorted);
        }
    }

    public class  ConfirmationKeyBuddy
    {
        [Display(ResourceType = typeof (Resources), Name = "ConfirmationKey_GuestGroupId")]
        [Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "ConfirmationKey_GuestGroupId_Required")]
        public int GuestGroupId { get; set; }

        [Display(ResourceType = typeof (Resources), Name = "ConfirmationKey_Key")]
        [Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "ConfirmationKeyBuddy_Key_Required")]
        public string Key { get; set; }

        [Display(ResourceType = typeof (Resources), Name = "ConfirmationKey_isReleased")]
// ReSharper disable InconsistentNaming
        public bool isReleased { get; set; }
// ReSharper restore InconsistentNaming

        [Display(ResourceType = typeof (Resources), Name = "ConfirmationKey_ConfirmationDate")]
        [DataType(DataType.DateTime)]
        public DateTime? ConfirmationDate { get; set; }

        [Display(ResourceType = typeof (Resources), Name = "ConfirmationKey_isConfirmed")]
// ReSharper disable InconsistentNaming
        public bool isConfirmed { get; set; }
// ReSharper restore InconsistentNaming

        [Display(ResourceType = typeof (Resources), Name = "ConfirmationKey_CreateDate")]
        [DataType(DataType.Date)]
        public DateTime? CreateDate { get; set; }

        [DataType(DataType.DateTime)]
        [Display(ResourceType = typeof (Resources), Name = "ConfirmationKey_ReleaseDate")]
        public DateTime? ReleaseDate { get; set; }

        [Display(ResourceType = typeof (Resources), Name = "ConfirmationKey_GuestGroupId")]
        public virtual GuestGroup GuestGroup { get; set; }
    }
}
